package com.tuju.currency.base

import android.graphics.drawable.AnimationDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.tuju.currency.R
import com.tuju.currency.utils.PerfectClickListener
import kotlinx.android.synthetic.main.activity_base.view.*

abstract class BaseParentActivity : BaseActivity() {

    lateinit var refreshView: View
    lateinit var loadingView: View
    lateinit var emptyView: View
    lateinit var mAnimationDrawable: AnimationDrawable
    lateinit var baseView: View
    lateinit var childView: View

    override fun setContentView(layoutResID: Int) {
        super.setContentView(layoutResID)
        baseView = LayoutInflater.from(this).inflate(R.layout.activity_base, null)
        childView = LayoutInflater.from(this).inflate(layoutResID, null)
        val params = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        childView.layoutParams = params
        var baseRelative = baseView.findViewById<RelativeLayout>(R.id.container)
        baseRelative.addView(childView)
        window.setContentView(baseView)

        loadingView = (findViewById<ViewStub>(R.id.vs_loading)).inflate()
        refreshView = (findViewById<ViewStub>(R.id.vs_error)).inflate()
        emptyView = (findViewById<ViewStub>(R.id.vs_empty)).inflate()
        val img = loadingView.findViewById<ImageView>(R.id.img_progress)

        // 加载动画
        mAnimationDrawable = img.drawable as AnimationDrawable
        // 默认进入页面就开启动画
        if (!mAnimationDrawable.isRunning) {
            mAnimationDrawable.start()
        }

        setToolBar()
        // 点击加载失败布局
        refreshView.setOnClickListener(object : PerfectClickListener() {
            override fun onNoDoubleClick(v: View) {
                showLoading()
                onRefresh()
            }
        })
        emptyView.setOnClickListener(object : PerfectClickListener() {
            override fun onNoDoubleClick(v: View) {
                showLoading()
                onRefresh()
            }
        })
        childView.setVisibility(View.GONE)
        refreshView.visibility = View.GONE
        emptyView.visibility = View.GONE
    }

    override fun setTitle(title: CharSequence?) {
        super.setTitle(title)
        baseView.tv_toolbar_title.setText(title)

    }

    fun setToolBar(){
        setSupportActionBar(baseView.tool_bar)
        val actionBar = supportActionBar
        if (actionBar != null) {
            //去除默认Title显示
            actionBar.setDisplayShowTitleEnabled(false)
            actionBar.setDisplayHomeAsUpEnabled(true)
            actionBar.setHomeAsUpIndicator(R.drawable.back_left_white)
        }
        baseView.tool_bar.setNavigationOnClickListener(View.OnClickListener { onBackPressed() })
    }

    protected fun setHideBackIcon() {
        supportActionBar?.setDisplayHomeAsUpEnabled(false)
        supportActionBar?.setHomeAsUpIndicator(null)
    }

    //显示加载中布局
    protected fun showLoading() {
        if (loadingView != null && loadingView.visibility != View.VISIBLE) {
            loadingView.visibility = View.VISIBLE
        }
        // 开始动画
        if (!mAnimationDrawable.isRunning) {
            mAnimationDrawable.start()
        }
        if (childView.visibility != View.GONE) {
            childView.visibility = View.GONE
        }
        if (refreshView.visibility != View.GONE) {
            refreshView.visibility = View.GONE
        }
        if (emptyView.visibility != View.GONE) {
            emptyView.visibility = View.GONE
        }
    }

    //显示内容布局
    protected fun showContentView() {
        if (loadingView != null && loadingView.visibility != View.GONE) {
            loadingView.visibility = View.GONE
        }
        // 停止动画
        if (mAnimationDrawable.isRunning) {
            mAnimationDrawable.stop()
        }
        if (refreshView.visibility != View.GONE) {
            refreshView.visibility = View.GONE
        }
        if (childView.visibility != View.VISIBLE) {
            childView.visibility = View.VISIBLE
        }
        if (emptyView.visibility != View.GONE) {
            emptyView.visibility = View.GONE
        }
    }

    //显示错误布局
    protected fun showError() {
        if (loadingView != null && loadingView.visibility != View.GONE) {
            loadingView.visibility = View.GONE
        }
        // 停止动画
        if (mAnimationDrawable.isRunning) {
            mAnimationDrawable.stop()
        }
        if (refreshView.visibility != View.VISIBLE) {
            refreshView.visibility = View.VISIBLE
        }
        if (childView.visibility != View.GONE) {
            childView.visibility = View.GONE
        }
        if (emptyView.visibility != View.GONE) {
            emptyView.visibility = View.GONE
        }
    }

    //显示空布局
    protected fun showEmpty(tips: String) {
        if (loadingView != null && loadingView.visibility != View.GONE) {
            loadingView.visibility = View.GONE
        }
        // 停止动画
        if (mAnimationDrawable.isRunning) {
            mAnimationDrawable.stop()
        }
        if (refreshView.visibility != View.GONE) {
            refreshView.visibility = View.GONE
        }
        if (childView.visibility != View.GONE) {
            childView.visibility = View.GONE
        }
        if (emptyView.visibility != View.VISIBLE) {
            //            mBaseBinding.tvEmptyTips.setText(tips);
            emptyView.visibility = View.VISIBLE
        }
    }

    //设置错误界面图片n
    fun setErrorViewImg(resourId: Int) {
        if (refreshView != null) {
            val errImg = refreshView.findViewById<ImageView>(R.id.iv_error)
            Glide.with(this).load(resourId).into(errImg)
        }
    }

    //设置错误界面提示
    fun setErrorViewTips(tips: String) {
        if (refreshView != null) {
            val errTips = refreshView.findViewById<TextView>(R.id.tv_error_tips)
            errTips.setText(tips)
        }
    }

    //设置空界面图片
    fun setEmptyViewImg(resourId: Int) {
        if (emptyView != null) {
            val emptyImg = emptyView.findViewById<ImageView>(R.id.iv_empty)
            Glide.with(this).load(resourId).into(emptyImg)
        }
    }

    //设置空界面提示
    fun setEmptyViewTips(tips: String) {
        if (emptyView != null) {
            val emptyTips = emptyView.findViewById<TextView>(R.id.tv_empty_tips)
            emptyTips.setText(tips)
        }
    }
    open fun onRefresh() {}
}